﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Collections;
using TvdbLib;
using TvdbLib.Data;
using Media.Core.Helpers.Main;
using Media.Core.Data;
using TvdbLib.Data.Banner;
using Media.Core.Logic;

namespace Media.Core.Helpers
{
    public class TvShowHelper
    {

        public static string Search(System.IO.DirectoryInfo[] dirInfos, string Username)
        {
            var Db = new Database();
            var TvEngine = new TvShowEngine(Db);
            try
            {
                TvdbHandler TvHandler = new TvdbHandler("73321F601FA4C423"); // API key for tvdb.com. User -> robrulez16 -> pass -> XXXX

                foreach (System.IO.DirectoryInfo d in dirInfos)
                {
                    //Tv Show Data
                    var Series = TvHandler.SearchSeries(d.Name.ToString()); // Searches for the tv Show. Gets first show found. Maybe implement a selection if needed...

                    var GotTv = TvEngine.CheckExists(Series[0].SeriesName);

                    if (GotTv == 1)
                    {
                        //Tv Show Data

                        TvdbSeries s = TvHandler.GetSeries(Series[0].Id, TvdbLanguage.DefaultLanguage, true, false, true, false);

                        var TvBannerSave = ImageGrabber.GetAndSave(GetDirectory.Location("Tv Banner Site") + s.BannerPath, s.SeriesName, s.SeriesName + " SeriesBanner", "Tv Banner");

                        ArrayList TvShowList = new ArrayList
                        {
                            new SqlParameter("ShowName", s.SeriesName),
                            new SqlParameter("Episodes", s.Episodes.Count),
                            new SqlParameter("Rating", s.Rating),
                            new SqlParameter("FirstAired", s.FirstAired),
                            new SqlParameter("Description", s.Overview),
                            new SqlParameter("BannerImage", TvBannerSave),
                            new SqlParameter("Status", s.Status)
                        };

                        var NewTvShow = Connections.GetDataRow("TvShow_Insert_New", TvShowList);
                        var TvId = Convert.ToInt32(NewTvShow["Id"].ToString());


                        // Seasons and Season Banners
                        foreach (TvdbSeasonBanner b in s.SeasonBanners)
                        {
                            var IsWatched = 0;
                            var TvSeasonBannerSave = ImageGrabber.GetAndSave(GetDirectory.Location("Tv Banner Site") + b.BannerPath, s.SeriesName, b.BannerPath, "Tv Season Banner");

                            ArrayList TvSeasonBanners = new ArrayList
                            {
                                new SqlParameter("SeriesName", s.SeriesName),
                                new SqlParameter("SeasonNumber", b.Season),
                                new SqlParameter("Image", TvSeasonBannerSave),
                                new SqlParameter("IsWatched", IsWatched)
                            };

                            var drSeasonInsert = Connections.GetDataRow("TvShow_Insert_Seasons", TvSeasonBanners);

                        }

                        //Tv Episode Data
                        foreach (TvdbEpisode e in s.Episodes)
                        {
                            var Airdate = e.FirstAired.ToString();
                            Airdate = (Airdate == "1/01/0001 12:00:00 AM") ? " 1/1/1800 12:00:00 AM" : Airdate;

                            ArrayList Episodes = new ArrayList
                            {
                                new SqlParameter("TvId", TvId),
                                new SqlParameter("TvDbId", e.Id),
                                new SqlParameter("SeasonNumber", e.SeasonNumber),
                                new SqlParameter("EpisodeNumber", e.EpisodeNumber), 
                                new SqlParameter("Name", e.EpisodeName),
                                new SqlParameter("AirDate", Convert.ToDateTime(Airdate)),
                                new SqlParameter("Overview", e.Overview), 
                                new SqlParameter("Rating", e.Rating)
                            };

                            var NewEpisode = Connections.GetDataRow("TvShow_Insert_Episodes", Episodes, "MovieDbContext");
                        }
                    }

                    //Inserts the TvShow Id to the User.
                    TvEngine.AddFileLocations(Series[0].SeriesName, Username, "TvShow", d.Name.ToString());
                }

                return "Added Successfully";
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }
        



    }
}
